package com.kl.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 自动填充处理类
 **/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        // 指定填充的字段名，字段类型和要填充的值
        // 乐观锁版本号的填充
        this.strictInsertFill(metaObject, "version", Integer.class, 1);
        // 填充逻辑字段的默认值
        this.strictInsertFill(metaObject, "deleteFlag", Integer.class, 0);
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }


    @Override
    public void updateFill(MetaObject metaObject) {
        // 指定要填充的字段名和要填充的值
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }
}